package gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import domain.Server;


public class ServerMainFrame extends JFrame {


	private static final long serialVersionUID = -2086484939524092688L;
	private GuiController gc;
	private JPanel mainPanel;
	
	public ServerMainFrame(GuiController gc){
		this.gc = gc;
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				initGUI();
				
			}
		});
	}

	private void initGUI() {
		System.out.println("initiating GUI");
		createFrame();
		addContentPanel();
		addControls();
		//openPopUpServerSettings();
		addSongTable();
	}
	
	private void createFrame()
	{
		this.setTitle("AudioBuddy Server 0.1");
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(800, 600);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
	}
	
	private void addContentPanel()
	{
		mainPanel = new JPanel(new BorderLayout());
		mainPanel.setSize(new Dimension(800, 600));
		this.setContentPane(mainPanel);
	}
	
	private void addControls()
	{
		JPanel controlPanel = new JPanel();
		controlPanel.setLayout(new FlowLayout());
		controlPanel.setBorder(BorderFactory.createLineBorder(Color.black));
		controlPanel.setSize(700,100);
		
		// Buttons
		JButton play = new JButton("Play");
		JButton pause = new JButton("Pause");
		JButton stop = new JButton ("Stop");
		JButton next = new JButton("Next");
		JButton prev = new JButton("Previous");		
		controlPanel.add(play);
		controlPanel.add(pause);
		controlPanel.add(stop);
		controlPanel.add(next);
		controlPanel.add(prev);
		
		// Currently playing
		JLabel playing = new JLabel("Currently playing:");
		controlPanel.add(playing);
		
		JLabel current = new JLabel("Johnny Cash - Mean as Hell");
		controlPanel.add(current);
		
		mainPanel.add(controlPanel, BorderLayout.NORTH);
	}
	
	private void addSongTable()
	{
		JTable songTable = new JTable(Server.getSongTableModel());
		songTable.setVisible(true);
		JPanel tablePanel = new JPanel();
		tablePanel.setVisible(true);
		tablePanel.add(songTable);
		songTable.repaint();
		mainPanel.add(tablePanel, BorderLayout.CENTER);
		
		JButton btnMulticastList = new JButton("send list to clients");
		btnMulticastList.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Server.MulticastPlaylist();
			}
		});
		
		mainPanel.add(btnMulticastList, BorderLayout.SOUTH);
	}

	private void openPopUpServerSettings() {
		PopUpSettingsFrame popup = new PopUpSettingsFrame(this);
		
	}

	public String getConnectionInfo() {
		return gc.getConnectionInfo();
	}

}
